package com.zlzxm.sorting.util.ui.window;

import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

/**
 * CREATE BY zlzxm ON 2022/3/8
 */
public abstract class AbsWindow extends DialogFragment {

    public abstract void  initRootView(RelativeLayout rlRoot);

    public abstract void initView(Dialog dialog);

    private boolean isOutSideTouchDismiss = false;
    RelativeLayout rlRoot;

    public void setOutSideTouchDismiss(boolean outSideTouchDismiss) {
        isOutSideTouchDismiss = outSideTouchDismiss;

        if(rlRoot != null) {
            if (isOutSideTouchDismiss) {

                rlRoot.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });

            }else {

                rlRoot.setOnClickListener(null);

            }
        }
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getContext());
         rlRoot = new RelativeLayout(getContext());
        rlRoot.setClipChildren(false);
        dialog.setContentView(rlRoot);
        if(isOutSideTouchDismiss){

            rlRoot.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });

        }

        initRootView(rlRoot);
        Window  window = dialog.getWindow();
        if(window!=null){

            window.setBackgroundDrawable(new ColorDrawable(0));

            window.setLayout(-1,-1);
        }

        initView(dialog);

        return dialog;
    }
}
